package loginnotice

import (
	"admin100/app/model"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/net/ghttp"
)

var LoginNoticeMgr = &loginNotcieMgr{}

type loginNotcieMgr struct{}

// @summary 批量修改登录公告
// @tags 工具
// @produce json
// @param   Id formData  string true "公告id列表, 1,3,6"
// @param   Notice formData  string true "内容"
// @router  /tool/loginnotice/batch-set [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *loginNotcieMgr) BatchSet(r *ghttp.Request) {
	var req model.BatchSetLoginNoticeReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	u := service.Context.GetUser(r.Context())
	service.LoginNotice.BatchSetNotice(u.Id, &req)
	response.JsonSuc(r)
}

// @summary 编辑登录公告
// @tags 工具
// @produce json
// @param   PlatformId formData  string true "平台ID"
// @param   ChannelId formData  string true "渠道ID"
// @param   Notice formData  string true "内容"
// @router  /tool/loginnotice/set [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *loginNotcieMgr) Set(r *ghttp.Request) {
	var req model.SetLoginNoticeReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	u := service.Context.GetUser(r.Context())
	if s := service.LoginNotice.UpdateAndPushLoginNotice(u.Id, &req); !s {
		// 这里报错99%由于http请求返回空字符串
		response.JsonFail(r)
	} else {
		response.JsonSuc(r)
	}
}

// @summary 登录公告列表
// @tags 工具
// @produce json
// @param   PlatformIdList formData  string true "平台id列表, 1,3,6"
// @router  /tool/loginnotice/list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *loginNotcieMgr) List(r *ghttp.Request) {
	var req model.LoginNoticeListReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	response.JsonSuc(r, service.LoginNotice.List(&req))
}

// @summary 批量删除登录公告
// @tags 工具
// @produce json
// @param   IdList formData  string true "公告id列表, 1,3,6"
// @router  /tool/loginnotice/del [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *loginNotcieMgr) Del(r *ghttp.Request) {
	var req model.DelLoginNoticeReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}

	u := service.Context.GetUser(r.Context())
	service.LoginNotice.Del(u.Id, &req)

	response.JsonSuc(r)
}
